package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.DataModule;
import com.ruoyi.system.domain.DataModuleType;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserVisualBase;
import com.ruoyi.system.service.DataModuleService;
import com.ruoyi.system.service.ISysModelManageService;
import com.ruoyi.system.vo.DataModuleAndRuleVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author tang ao long
 * @Date 2020-08-24 16:38
 * @Version 1.0
 */
@Controller
@RequestMapping("/system/DataModule")
public class SysDataModuleController extends BaseController {

    @Autowired
    private DataModuleService dataModuleService;

    @Autowired
    private ISysModelManageService modelManageService;

    private String prefix = "system/datamodule";

    @RequiresPermissions("system:dataModule:view")
    @GetMapping()
    public String userVisualBase(ModelMap modelMap)
    {
        List<DataModuleType> moduleTypes = modelManageService.selectAllModelType(null);
        modelMap.put("moduleTypes",moduleTypes);
        return prefix + "/dataModule";
    }

    /**
     * 查询数据模型信息
     * @param dataModule 查询对象
     * @return 返回查询结果
     */
    @RequiresPermissions("system:dataModule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataModule dataModule)
    {
        startPage();
        List<DataModule> dataModuleList = dataModuleService.selectDataModuleListByInfo(dataModule);
        return getDataTable(dataModuleList);
    }

    /**
     * 删除大屏信息
     * @param ids 待删除的ID列
     * @return 返回删除结果
     */
    @RequiresPermissions("system:dataModule:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dataModuleService.deleteDataModuleByIds(ids));
    }

    /**
     * 新增模型类型页面
     */
    @RequiresPermissions("system:dataModule:add")
    @Log(title = "模型管理", businessType = BusinessType.INSERT)
    @GetMapping("/add")
    public String add(ModelMap mp)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        List<DataModuleType> moduleTypes = modelManageService.selectAllModelType(null);
        mp.put("moduleTypes",moduleTypes);
        mp.put("user",user);
        return prefix + "/add";
    }

    @RequiresPermissions("system:dataModule:add")
    @Log(title = "新增数据模型对象", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated DataModuleAndRuleVO dataModuleAndRuleVO,@RequestParam("file") MultipartFile file) throws IOException {
        //文件上传
        String fileName = FileUploadUtils.upload(file);
        dataModuleAndRuleVO.setPicUrl(fileName.substring(1));
        return toAjax(dataModuleService.insertDataModuleInfo(dataModuleAndRuleVO));
    }

    /**
     * 检查模型名称是否重复
     */
    @PostMapping("/checkDataModuleNameUnique")
    @ResponseBody
    public Boolean checkVisualBaseNameUnique(@Validated String id,@Validated String moduleName){
        return dataModuleService.checkDataModuleNameUnique(moduleName,id) <= 0;
    }

    /**
     * 修改品牌信息
     */
    @RequiresPermissions("system:dataModule:edit")
    @Log(title = "数据模型管理", businessType = BusinessType.UPDATE)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        List<DataModuleType> moduleTypes = modelManageService.selectAllModelType(null);
        mmap.put("moduleTypes",moduleTypes);
        mmap.put("user",user);
        mmap.put("dataModule", dataModuleService.selectDataModuleInfoById(id));
        return prefix + "/edit";
    }

    @RequiresPermissions("system:dataModule:edit")
    @Log(title = "修改数据模型对象", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated DataModuleAndRuleVO dataModuleAndRuleVO,@RequestParam("file") MultipartFile file) throws IOException {

        if(!file.isEmpty()){
            System.out.println(file.getName());
            //文件上传
            String fileName = FileUploadUtils.upload(file);
            dataModuleAndRuleVO.setPicUrl(fileName.substring(1));
        }

        return toAjax(dataModuleService.updateDataModuleInfo(dataModuleAndRuleVO));
    }

    @GetMapping("/modelType")
    public String showModelTypePage(){
        return prefix + "/modelType";
    }
}
